package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemProcessRecordDto;
import com.jyu.apts.dto.ItemProcessRecordInfoDto;
import com.jyu.apts.dto.QRCodeDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.entity.ItemBatchRecord;
import com.jyu.apts.entity.ItemProcessRecord;
import com.jyu.apts.entity.ItemProcessRecordInfo;
import com.jyu.apts.entity.QRCode;
import com.jyu.apts.entityEnum.QRCodeTypeEnum;
import com.jyu.apts.service.ItemProcessRecordService;
import com.jyu.apts.service.QRCodeService;

@RestController
public class OriginController {

	@Autowired
	QRCodeService qrCodeService;
	@Autowired
	ItemProcessRecordService itemProcessRecordService;
	
	@RequestMapping("/getQrCodeInfo")
	public ModelAndView getQrCodeInfo(long id){
		QRCode qrCode = qrCodeService.getOne(id);
		if(qrCode == null) {
			return null;
		}
		else {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("/originInfo.html");
			mv.addObject("qrCode", new QRCodeDto().convert(qrCode));
			return mv;
		}
	}
	
	@RequestMapping("/findItemProcessRecordByOriginCode")
	public ResultDto<List<ItemProcessRecordDto>> findItemProcessRecordByOriginCode(String originCode){
		QRCode qrCode = qrCodeService.findByCode(originCode);
		if(qrCode == null) {
			return new ResultDto("无效溯源码",null,2);
		}
		else if(qrCode.getType()!=QRCodeTypeEnum.ORIGIN.getType()) {
			return new ResultDto("非溯源码",null,2);
		}
		ItemBatchRecord itemBatchRecord = qrCode.getItemBatchRecord();
		List<ItemProcessRecordDto> itemProcessRecordDtos = new ArrayList<ItemProcessRecordDto>();
		List<ItemProcessRecord> itemProcessRecords = itemBatchRecord.getItemProcessRecords();
		itemProcessRecords.forEach(itemProcessRecord->itemProcessRecordDtos.add(new ItemProcessRecordDto().convert(itemProcessRecord)));
		return new ResultDto("查询成功",itemProcessRecords,0);
	}
	
	@RequestMapping("/findNormalQRCodesByItemProcessRecord")
	public ResultDto<List<QRCodeDto>> findNormalQRCodesByItemProcessRecord(long itemProcessRecordId){
		ItemProcessRecord itemProcessRecord = itemProcessRecordService.getOne(itemProcessRecordId);
		if(itemProcessRecord == null) {
			return new ResultDto("流程记录不存在",null,2);
		}
		else {
			List<QRCodeDto> qrCodeDtos = new ArrayList<QRCodeDto>();
			List<QRCode> qrCodes = itemProcessRecord.getNormalQRCodes();
			qrCodes.forEach(qrCode->qrCodeDtos.add(new QRCodeDto().convert(qrCode)));
			return new ResultDto("获取成功",qrCodeDtos,0);
		}
	}
	
	@RequestMapping("/findItemProcessRecordInfoByItemProcessRecord")
	public ResultDto<List<ItemProcessRecordInfoDto>> findItemProcessRecordInfoByItemProcessRecord(long itemProcessRecordId){
		ItemProcessRecord itemProcessRecord = itemProcessRecordService.getOne(itemProcessRecordId);
		if(itemProcessRecord == null) {
			return new ResultDto("流程记录不存在",null,2);
		}
		else {
			List<ItemProcessRecordInfoDto> itemProcessRecordInfoDtos = new ArrayList<ItemProcessRecordInfoDto>();
			List<ItemProcessRecordInfo> itemProcessRecordInfos = itemProcessRecord.getItemProcessRecordInfos();
			itemProcessRecordInfos.forEach(itemProcessRecordInfo->itemProcessRecordInfoDtos.add(new ItemProcessRecordInfoDto().convert(itemProcessRecordInfo)));
			return new ResultDto("获取成功",itemProcessRecordInfoDtos,0);
		}
	}
}
